Comparison
bool result = (12 != 12)
bool result = (41.1 <= 42)
bool result = (41.1 >= 42)
bool result = (4 < 7)
bool result = (3.1 > 3.1)
bool result = (11 == 8)
Sample usage
Not equal to
Less than or equal to
Greater than or equal to
Less than
Greater than
Equal to
Meaning
false
true
false
true
false
false
Result
!=
<=
>=
<
>
==
C
C has no special type to represent logical or boolean values. It improvises by using any of the integral types char, int, short, long, unsigned, with a value of 0 representing false and any other value representing true. It is rare for logical values to be stored in variables. They are usually generated as required by comparing two numeric values. This is where the comparison operators are used, to compare two numeric values and produce a logical result.
Note that == is used in comparisons and = is used in assignments. Comparison operators are used in expressions like the ones below.
In the last example, all arithmetic is done before any comparison is made.
These comparisons are usually used to control an if statement or a for or a while loop. These will be introduced in a later chapter.